package com.dyzx.hbb.modules.sys.service.impl;

import cn.hutool.extra.pinyin.PinyinUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dyzx.hbb.modules.sys.entity.SysMessages;
import com.dyzx.hbb.modules.sys.service.SysMessagesService;
import com.dyzx.hbb.modules.sys.mapper.SysMessagesMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
* @author Administrator
* @description 针对表【sys_messages】的数据库操作Service实现
* @createDate 2025-08-21 12:50:23
*/
@Service
public class SysMessagesServiceImpl extends ServiceImpl<SysMessagesMapper, SysMessages>
    implements SysMessagesService{

    @Override
    public boolean addMessage(String name) {

        System.out.println("--------------------"+name+"--------------------");
        System.out.println(name);
        String pinyin =isChinese(name)? PinyinUtil.getPinyin(name,""):name;
        pinyin= StringUtils.trimToEmpty(pinyin);
        pinyin=StringUtils.replace(pinyin," ","");
        pinyin=StringUtils.replace(pinyin,",","");
        pinyin=StringUtils.replace(pinyin,".","");
        pinyin=StringUtils.replace(pinyin,"-","");
        pinyin=StringUtils.replace(pinyin,"/","");
        pinyin=StringUtils.replace(pinyin,"_","");
        pinyin=StringUtils.replace(pinyin,"(","");
        pinyin=StringUtils.replace(pinyin,")","");
        pinyin=StringUtils.replace(pinyin,")","");
        pinyin=StringUtils.replace(pinyin,"+","");
        pinyin=StringUtils.replace(pinyin,"*","");
        pinyin=StringUtils.replace(pinyin,"\"","");
        pinyin=StringUtils.replace(pinyin,"'","");
        pinyin=StringUtils.replace(pinyin,";","");
        pinyin=StringUtils.replace(pinyin,":","");
        pinyin=StringUtils.replace(pinyin,"!","");
        pinyin=StringUtils.replace(pinyin,"?","");
        pinyin=StringUtils.replace(pinyin,"@","");
        pinyin=StringUtils.replace(pinyin,"。","");
        pinyin=StringUtils.replace(pinyin,"，","");
        pinyin=StringUtils.replace(pinyin,"、","");
        pinyin=StringUtils.replace(pinyin,"“","");
        pinyin=StringUtils.replace(pinyin,"”","");
        System.out.println("--------------------"+pinyin+"--------------------");
        LambdaQueryWrapper<SysMessages> query = new LambdaQueryWrapper<>();
        query.eq(SysMessages::getCode,pinyin);
        if (this.count(query)==0) {
            SysMessages messages=new SysMessages();
            messages.setCode(pinyin);
            messages.setZhCn(name);
            this.save(messages);
        }
        return false;
    }

    public boolean isChinese(String strName) {
        char[] ch = strName.toCharArray();
        for (char c : ch) {
            if (isChinese(c)) {
                return true;
            }
        }
        return false;
    }

    private boolean isChinese(char c) {
        Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
        if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
                || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
                || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
                || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {
            return true;
        }
        return false;
    }
}




